//
//  SSDKTrace.m
//  SuperSDK
//
//  Created by 涂俊 on 2017/10/30.
//  Copyright © 2017年 WYHT. All rights reserved.
//

#import "SSDKTrace.h"
#import "SSDKHttpV2Util.h"
#import "SSDKEventKeys.h"
#import "SSDKEventManager.h"
#import "SSDKDataKeys.h"

#import "SuperSDKInst.h"
#import "SSDKUserManager.h"
#import "SSDKUserInfo.h"
#import "SSDKSwitchUtil.h"

#import "SuperSDKPluginTools/SSDKPluginDataHelper.h"

@implementation SSDKTrace

// 缓存上报登录数据的key
static NSString *CACHE_LOGIN_SUBMIT_KEY= @"login_submit";

//上报数据的地址
static NSString *SDK_TRACE_PATH = @"/supersdk-web/v2/app/trace/submit";

-(void) initInfo {
    [SSDKEventManager addObsver:self selector:@selector(submitData:) name:EVENT_SUBMITDATA object:nil className:@"SSDKTrace"];
}

-(void) submitData:(NSNotification*) notification {
    NSString *ext = [[notification object] objectForKey:SUBMIT_EXT];
    // 缓存登录上报数据(用于心跳)
    if([SUBMIT_LOGIN_EVENT isEqualToString:ext]) {
        NSLog(@"缓存登录上报数据");
        [SSDKPluginDataHelper setDataValue:[notification object] forKey:CACHE_LOGIN_SUBMIT_KEY];
    }
    [SSDKSwitchUtil valueWithKey:@"submit_switch" delegate:^(NSString *value) {
        // 只有服务器开关开启，才开启上报数据，默认是关闭
        if (![@"true" isEqualToString:value]) {
            NSLog(@"上报数据功能关闭！");
            return;
        }
        //    NSString *url = [@"http://192.168.2.150" stringByAppendingString:SDK_TRACE_PATH];
        NSString *url = [[[SuperSDKInst getInstance] sUrl] stringByAppendingString:SDK_TRACE_PATH];
        if([SUBMIT_LOGIN_EVENT isEqualToString:ext] ||//登录事件
           [SUBMIT_CREATE_ROLE_EVENT isEqualToString:ext] ||//创建角色
           [SUBMIT_LEVEL_UP_EVENT isEqualToString:ext] ||//角色升级
           [SUBMIT_FINISH_GUIDE_EVENT isEqualToString:ext] ||//完成新手引导
           [SUBMIT_PAY_EVENT isEqualToString:ext]) {//支付成功事件
            NSLog(@"%@,上报数据", ext);
            NSMutableDictionary *params = [notification object];
            NSMutableDictionary *wrapParams = [[NSMutableDictionary alloc] initWithDictionary:params];
            SSDKUserInfo *user = [[SSDKUserManager getInstance] curUserInfo];
            [wrapParams setValue:[[SuperSDKInst getInstance] appId] forKey:APP_ID];
            [wrapParams setValue:[[SuperSDKInst getInstance] channelId] forKey:CHANNEL_ID];
            [wrapParams setValue:[NSString stringWithFormat:@"%d", [[SuperSDKInst getInstance] serverID]] forKey:SUBMIT_ZONE_ID];
            [wrapParams setValue:[user superSDKUid] forKey:SUPERSDK_UID];
            [wrapParams setValue:[user sdkUid] forKey:SDK_UID];
            [wrapParams setValue:@"IOS" forKey:@"platform"];
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:SSDK_L_DEVICE_ID] forKey:SSDK_L_DEVICE_ID];
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:DEVICE_VERSION] forKey:@"device_type"];
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:SSDK_L_IDFA] forKey:SSDK_L_IDFA];
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:APP_INNER_VERSION] forKey:APP_INNER_VERSION];
            [wrapParams setValue:[[[SuperSDKInst getInstance] clientInfo] valueForKey:OS_VERSION] forKey:OS_VERSION];
            //如果有手机号，则上报数据中带上手机号
            if ([[SuperSDKInst getInstance] mobileNumber] != nil && [[SuperSDKInst getInstance] mobileNumber].length > 0) {
                [wrapParams setValue:[[SuperSDKInst getInstance] mobileNumber] forKey:SUBMIT_PHONE];
            }
            [SSDKHttpV2Util postWithUrl:url data:wrapParams httpDelegate:^(BOOL status, NSDictionary *resData) {
                NSLog(@"%@,上报结果:%@", ext, status?@"ok":@"fail");
            }];
        }
    }];
}
@end
